// Includes
#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>

// Defines
#define trigPin 4
#define echoPin 2

// Global variables
const uint64_t pipe = 0xE8E8F0F0E1LL;
RF24 radio(9,10);
int msg[1];

void setup() {
  Serial.begin (9600);
  radio.begin();
  radio.openWritingPipe(pipe);
  
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  
  if (distance < 15) {  // This is where the LED On/Off happens
    Serial.println("Too close!"); // When the Red condition is met, the Green LED should turn off
    msg[0] = 0;
    radio.write(msg, 1);
    Serial.println("0");
  }
  else {
    
  }
  if (distance >= 200 || distance <= 0){
    Serial.println("Out of range");
    msg[0] = 2;
    radio.write(msg, 1);
    Serial.println(2);
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(500);
}

